﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeEdu.DAL.DataAccess
{
    using LeEdu.DTO;
    using ServiceStack.OrmLite;

    /// <summary>
    /// 数据库管理类
    /// </summary>
    public class DataManager: BaseDAL
    {
        private HourDal hourDal;
        public DataManager()
        {
            hourDal = new HourDal();
        }

        /// <summary>
        /// 初始化数据库
        /// </summary>
        /// <returns></returns>
        public bool Initial()
        {
            using (var cnn=DbConnection)
            {
                //用户信息表
                cnn.DropAndCreateTable<UserInfoDTO>();

                //添加测试用户
                var usr = new UserInfoDTO() { 
                    Id = new Guid("21164899-43DB-4851-92EC-C5983EE48DA2"), 
                    UserName = "user", 
                    Password = "user",
                    RoleId=2
                };

                cnn.Insert<UserInfoDTO>(usr);
                //添加测试管理员
                usr = new UserInfoDTO()
                {
                    Id = new Guid("3A506760-D3E2-4D8C-9432-C910ED849E41"),
                    UserName = "admin",
                    Password = "admin",
                    RoleId = 1
                };
                cnn.Insert<UserInfoDTO>(usr);

                //课程表
                cnn.DropAndCreateTable<Course>();
                cnn.DropAndCreateTable<File>();
                //章节
                cnn.DropAndCreateTable<Node>();
                cnn.DropAndCreateTable<Hour>();

                //添加测试数据
                //课程
                Course cs = new Course();
                cs.Id = Guid.NewGuid();
                cs.Name = "LINQ解惑";
                cs.Description = "详解LINQ基础知识及在项目中的应用";
                cs.CreateTime = DateTime.Now;
                cs.ImgUrl = "/Uploads/Courses/CoursesImg/d897accf-d75c-46c4-af54-1e6428f0300c.jpg";
                cnn.Insert<Course>(cs);

                //添加课时
                //第一章
                var chapter = getNode("Linq入门", Guid.NewGuid(), null, cs.Id, NodeType.Chapter, 1);
                cnn.Insert<Node>(chapter);
                var section = getNode("Linq简介", Guid.NewGuid(), chapter.Id, cs.Id, NodeType.Hour, 2);
                insertHour(section);
                section = getNode("Linq入门示例", Guid.NewGuid(), chapter.Id, cs.Id, NodeType.Hour, 3);
                insertHour(section);
                //第二章
                chapter = getNode("Linq精通", Guid.NewGuid(), null, cs.Id, NodeType.Chapter, 4);
                cnn.Insert<Node>(chapter);
                section = getNode("Linq高级查询", Guid.NewGuid(), chapter.Id, cs.Id, NodeType.Section, 5);
                cnn.Insert<Node>(section);
                section = getNode("Linq聚合类", Guid.NewGuid(), chapter.Id, cs.Id, NodeType.Hour, 6);
                insertHour(section);
                section = getNode("Linq排序类", Guid.NewGuid(), chapter.Id, cs.Id, NodeType.Hour, 7);
                insertHour(section);

                //第三章
                chapter = getNode("Linq实战", Guid.NewGuid(), null, cs.Id, NodeType.Chapter, 8);
                cnn.Insert<Node>(chapter);
                section = getNode("Linq项目应用01", Guid.NewGuid(), chapter.Id, cs.Id, NodeType.Hour, 9);
                insertHour(section);
                section = getNode("Linq项目应用02", Guid.NewGuid(), chapter.Id, cs.Id, NodeType.Hour, 10);
                insertHour(section);
            }
            return true;
        }

        private Node getNode(string name, Guid id,Nullable<Guid> parentId, Guid cid, NodeType nt, int order)
        {
            Node nd = new Node();
            nd.Id = Guid.NewGuid();
            nd.Order = order;
            nd.Name = name;
            nd.CourseId = cid;
            nd.CreateTime = DateTime.Now;
            nd.ParentId = parentId;
            nd.Type = nt;
            return nd;
        }

        private bool insertHour(Node node)
        {
            using (var cnn=DbConnection)
            {
                cnn.Insert<Node>(node);
                var hour= getHour(node);
                cnn.Insert<Hour>(hour);

                return true;
            }
        }

        private Hour getHour(Node node)
        {
            Hour hour = new Hour();
            hour.Id = node.Id;
            hour.CreateTime = DateTime.Now;

            return hour;
        }

    }
}
